<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller {
	
	//加载登陆视图
    public function index(){
    	$this->display();
    }

    //执行登陆
    public function dologin(){
    	//初始化变量
    	$username = I('post.username');
    	$pass = I('post.pass');
    	$jizhu = I('post.jizhu');
    	$user = M('user');
    	//查询是否存在该用户
    	$res = $user->where('username='."'$username'")->find();

    	if($res){
    		//判断密码
    		if(md5($pass) == $res['pass']){
    			//是否记住密码
    			if(empty($jizhu)){
    				session('user',$res);
    			}else{
    				//设置session和cookie存活时间
    				session('user',$res);
    				setcookie('user',$res,time()+3600*24);
    			}
    			
    			$this->success('登陆成功',U('/Home/Index/index'));
    		}else{
    			$this->error('密码错误');
    		}
    	}else{
    		$this->error('用户名不存在');
    	}
    }

    //退出登陆
    public function jin(){
    	session('user',null);
    	$this->success('注销成功',U('/Home/Login/index'));
    }
}